package dao.daoimpl;

import dao.*;
import model.*;
import util.DBTool;
import java.sql.*;
import java.util.Calendar;

/**
 * Created by 杨珂 on 2016/8/9.
 * 用户卡关系表Dao实现
 */
public class UserCardDaoImpl implements UserCardDao {
	@Override	
	public int insertUserCard(UserCard usercard,int count) {
		//检查是否已经存在，如果是则执行续费操作
		UserCard uc = this.selectUserCardById(usercard.getUserId(), usercard.getCardType());
		//如果uc为空，说明表中不含有该关系，否则执行更新操作
		if(uc!=null) {
			if(usercard.getCardType()==1)
				//续费年卡
				this.update(uc, 1, 0);
			else if(usercard.getCardType()==2)
				//续费次卡
				this.update(uc, 3, count);
			//返回-1代表没有新插入语句
			return -1;
		}
		
		int nextid = 0;//下一个可用的user_card_id值，因为user_card_id是递增的
		Connection conn = null;
		PreparedStatement prst = null;
		String insertsql = "insert into user_card (user_card_id,user_id,card_type,open_time,end_time,count) values(?,?,?,?,?,?)";
		String getIdsql = "select max(user_card_id) as id from iyoga.user_card; ";
		conn = DBTool.getConnection();
		PreparedStatement getNextUserCourseId = null;
		try {
			// 取出当前最大id来设置当前id
			getNextUserCourseId = conn.prepareStatement(getIdsql);
			ResultSet rs = getNextUserCourseId.executeQuery();
			if (rs.next()) {
				nextid = rs.getInt("id") + 1;
			}
			//插入语句
			prst = conn.prepareStatement(insertsql);
			prst.setInt(1, nextid);
			prst.setInt(2, usercard.getUserId());
			prst.setInt(3, usercard.getCardType());
			prst.setDate(4, new java.sql.Date(usercard.getOpenTime().getTime()));
			prst.setDate(5, new java.sql.Date(usercard.getEndTime().getTime()));
			prst.setInt(6,count);

			prst.executeUpdate();

		} catch (SQLException ex) {
			ex.printStackTrace();
			//打印异常
			System.out.println(ex);
		} finally {
			DBTool.close(prst, conn);
		}
		return nextid;
	}
	@Override
	public UserCard selectUserCardById(int userId, int cardType) {
		//通过id查找必然只有一个对象，故返回UserCard
		Connection conn = null;
		PreparedStatement prst = null;
		String selectByIdsql = "select * from iyoga.user_card where user_id=? and card_type=?; ";
		conn = DBTool.getConnection();
		try {
			prst = conn.prepareStatement(selectByIdsql);
			prst.setInt(1, userId);
			prst.setInt(2, cardType);
			ResultSet rs = prst.executeQuery();
			if (rs.next()) {
				UserCard usercard = new UserCard();
				usercard.setUserCardId(rs.getInt("user_card_id"));
				usercard.setUserId(rs.getInt("user_id"));
				usercard.setCardType(rs.getInt("card_type"));
				usercard.setOpenTime(new java.sql.Date(rs.getDate("open_time").getTime()));
				usercard.setEndTime(new java.sql.Date(rs.getDate("end_time").getTime()));
				usercard.setCount(rs.getInt("count"));
				return usercard;
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			DBTool.close(prst, conn);
		}
		return null;
	}
	@Override
	//year代表增加的年份，count代表增加的次数，续费卡，返回修改的行数
	public int update(UserCard userCard, int year, int count) {

		Connection conn = null;
		PreparedStatement prst = null;
		String selectByIdsql = "UPDATE iyoga.user_card SET end_time=?,count=? WHERE user_card_id = ? ; ";
		conn = DBTool.getConnection();
		try {
			//修改结束时间和次数
			prst = conn.prepareStatement(selectByIdsql);
			Calendar rightNow = Calendar.getInstance();
	        rightNow.setTime(userCard.getEndTime());
	        for(int i=0;i<year;++i)
	        	rightNow.add(Calendar.YEAR,+1);
	        java.util.Date newendTime = rightNow.getTime();
	        
	        
			prst.setDate(1, new java.sql.Date(newendTime.getTime()));
			prst.setInt(2, count+userCard.getCount());
			prst.setInt(3, userCard.getUserCardId());
			
			return prst.executeUpdate();//返回修改的行数
			

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			DBTool.close(prst, conn);
		}
		return 0;
	}
}
